package com.zret.oasys.operation.emp;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.zret.oasys.entity.Emp;

public class DeleteEmp implements EmpServletOperation {

	@Override
	public void doOperation(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 通过id删除员工
		// 1.设置json编码
		response.setContentType("application/json;charset=utf-8");
		// 2.从jsp获取empname,获取查询到的list
		String empid = request.getParameter("empid");
		System.out.println(empid);
		if ("".equals(empid)) {
			List<Emp> list = empService.getEmps();
			// 3.创建输出流
			PrintWriter out = response.getWriter();
			// 4.创建Gson对象
			Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
			// 5.将查询到的员工list转为字符串
			String jsonString = gson.toJson(list);
			// 6.控制台测试打印
			System.out.println(jsonString);
			// 7.传输给jsp页面
			out.print(jsonString);
			// 8.关闭out
			out.close();
		} else {
			int id = Integer.valueOf(empid);
			boolean flag = empService.deleteEmps(id);
			PrintWriter out = response.getWriter();
			Gson gson = new Gson();
			String msg1 = gson.toJson("删除员工成功");
			String msg2 = gson.toJson("删除员工失败");
			if (flag) {
				out.print(msg1);
			} else {
				out.print(msg2);
			}
			out.close();
		}
	}
}
